import React, { Component } from 'react'
import PropTypes from 'prop-types'
import _ from 'loadsh'
import moment from 'moment'
import { TitleLine, TextLine, TextCell } from '@components/ApprovePre/Approve'

export default class StepFourHistory extends Component {
  state = {
    keyTitleMap: {}
  }

  initKeyTitleMap () {
    const map = {}
    const list = _.get(this.props.action || {}, 'vars', [])
    list.forEach(li => {
      map[li.key] = li.title
    })
    this.setState({
      keyTitleMap: map
    })
  }

  formatDate (t) {
    if (!t) {
      return t
    }
    return moment(t * 1000).format('YYYY-MM-DD HH:mm')
  }

  componentDidUpdate (prev) {
    if (this.props.instance !== prev.instance) {
      this.initKeyTitleMap()
    }
  }

  componentDidMount () {
    this.initKeyTitleMap()
  }

  render () {
    const { action } = this.props
    const { varsMap } = action
    const { keyTitleMap } = this.state
    return (
      <div>
        <TitleLine title={this.props.action.step_name} operator={action.operator} time={this.formatDate(action.complete_datetime)} />
        <TextLine>
          <TextCell style={{ width: '65%' }}>{keyTitleMap.closure_arbitrator_feedback}：{(varsMap.closure_arbitrator_feedback || {}).value || '无'}</TextCell>
        </TextLine>
        <TextLine>
          <TextCell style={{ width: '65%' }}>{keyTitleMap.closure_arbitrator_feedback2}：{(varsMap.closure_arbitrator_feedback2 || {}).value || '无'}</TextCell>
        </TextLine>
        {this.props.renderDocument()}
        <TextLine>
          <TextCell style={{ width: '65%' }}>{action.remark_alias || '备注'}：{action.remark || '无'}</TextCell>
        </TextLine>
      </div>
    )
  }
}

StepFourHistory.proptypes = {
  userInfo: PropTypes.object,
  instance: PropTypes.object,
  action: PropTypes.object,
  renderDocument: PropTypes.func
}

StepFourHistory.defaultProps = {
  userInfo: {},
  instance: {},
  action: {},
  renderDocument: () => { }
}
